﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace ActiveDirectoryTester
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            try
            {
                string groups = "";
                if (IsAuthenticated(tb_ActiveDirectoryPath.Text.Trim(), tb_ActiveDirectoryDomain.Text.Trim(), tb_UserName.Text.Trim(), tb_Password.Text.Trim(), ref  groups))
                    MessageBox.Show("Success");
                else
                    MessageBox.Show("Failed");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        bool IsAuthenticated(string adPath, string domainName, string userName, string password, ref string groups)
        {
            string _filterAttribute = "";

            {
                string domainAndUsername = domainName + @"\" + userName;
                DirectoryEntry entry = new DirectoryEntry(adPath, domainAndUsername, password);
                // Bind to the native AdsObject to force authentication.
                Object obj = entry.NativeObject;
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + userName + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
                if (null == result)
                {
                    return false;
                }
                // Update the new path to the user in the directory
                adPath = result.Path;
                _filterAttribute = (String)result.Properties["cn"][0];
                return true;
            }

            //{
            //    DirectorySearcher search = new DirectorySearcher(adPath);
            //    search.Filter = "(cn=" + _filterAttribute + ")";
            //    search.PropertiesToLoad.Add("memberOf");
            //    StringBuilder groupNames = new StringBuilder();
            //    SearchResult result = search.FindOne();
            //    int propertyCount = result.Properties["memberOf"].Count;
            //    string dn;
            //    int equalsIndex, commaIndex;

            //    for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
            //    {
            //        dn = (string)result.Properties["memberOf"][propertyCounter];
            //        equalsIndex = dn.IndexOf("=", 1);
            //        commaIndex = dn.IndexOf(",", 1);
            //        if (-1 == equalsIndex)
            //        {
            //            groups = "";
            //            return true;
            //        }
            //        groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
            //        groupNames.Append("|");
            //    }
            //    groups = groupNames.ToString();
            //    return true;
            //}
        }
    }
    
}